﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using System.Reflection;
using log4net;
using Microsoft.ApplicationBlocks.Data;
using Microsoft.VisualBasic;

namespace AnyOffice.general.person_info
{
    public partial class mytable_config :  ModulePage
    {


        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                ListItem item;
                DataRow row;
                string str = (Request.QueryString["pos"] != null) ? Request.QueryString["pos"].ToString() : "left";
                this.ViewState["Pos"] = str;
                if (str == "left")
                {
                    this.litNavTitle.Text = "左侧桌面";
                }
                else
                {
                    this.litNavTitle.Text = "右侧桌面";
                }
                string str2 = string.Empty;
                string str3 = string.Empty;
                DataTable myTable = ApplicationCache.MyTable;
                myTable.PrimaryKey = new DataColumn[] { myTable.Columns["ModuleId"] };
                for (int i = 0; i < myTable.Rows.Count; i++)
                {
                    if (Convert.ToInt32(myTable.Rows[i]["ViewType"].ToString()) != 3)
                    {
                        if (myTable.Rows[i]["ModulePos"].ToString() == "l")
                        {
                            str2 = str2 + myTable.Rows[i]["ModuleId"].ToString() + ",";
                        }
                        else
                        {
                            str3 = str3 + myTable.Rows[i]["ModuleId"].ToString() + ",";
                        }
                    }
                }
                if (str2.EndsWith(","))
                {
                    str2 = str2.Substring(0, str2.Length - 1);
                }
                if (str3.EndsWith(","))
                {
                    str3 = str3.Substring(0, str3.Length - 1);
                }
                string commandText = "SELECT [MytableLeft],[MytableRight] FROM [dbo].[Users] WHERE [UserId] = '" + base.User.Identity.Name + "'";
                DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText).Tables[0];
                if (table2.Rows.Count > 0)
                {
                    if (table2.Rows[0]["MytableLeft"] != DBNull.Value)
                    {
                        str2 = table2.Rows[0]["MytableLeft"].ToString();
                    }
                    if (table2.Rows[0]["myTableRight"] != DBNull.Value)
                    {
                        str3 = table2.Rows[0]["MyTableRight"].ToString();
                    }
                }
                if ((str == "left") && (str2 != string.Empty))
                {
                    string[] strArray = str2.Split(new char[] { ',' });
                    for (int k = 0; k < strArray.Length; k++)
                    {
                        if (Information.IsNumeric(strArray[k]))
                        {
                            item = new ListItem();
                            item.Value = strArray[k];
                            row = myTable.Rows.Find(Convert.ToInt32(strArray[k]));
                            if ((row != null) && (Convert.ToInt32(row["ViewType"]) != 3))
                            {
                                item.Text = row["ModuleName"].ToString();
                                if (Convert.ToInt32(row["ViewType"].ToString()) == 2)
                                {
                                    item.Text = item.Text + "[必选]";
                                }
                                this.pklMyTable.ItemsLeft.Add(item);
                            }
                        }
                    }
                }
                if ((str == "right") && (str3 != string.Empty))
                {
                    string[] strArray2 = str3.Split(new char[] { ',' });
                    for (int m = 0; m < strArray2.Length; m++)
                    {
                        if (Information.IsNumeric(strArray2[m]))
                        {
                            item = new ListItem();
                            item.Value = strArray2[m];
                            row = myTable.Rows.Find(Convert.ToInt32(strArray2[m]));
                            if ((row != null) && (Convert.ToInt32(row["ViewType"]) != 3))
                            {
                                item.Text = row["ModuleName"].ToString();
                                if (Convert.ToInt32(row["ViewType"].ToString()) == 2)
                                {
                                    item.Text = item.Text + "[必选]";
                                }
                                this.pklMyTable.ItemsLeft.Add(item);
                            }
                        }
                    }
                }
                string str5 = "," + str2 + "," + str3 + ",";
                for (int j = 0; j < myTable.Rows.Count; j++)
                {
                    if ((str5.IndexOf("," + myTable.Rows[j]["ModuleId"].ToString() + ",") == -1) && (Convert.ToInt32(myTable.Rows[j]["ViewType"].ToString()) != 3))
                    {
                        item = new ListItem();
                        item.Value = myTable.Rows[j]["ModuleId"].ToString();
                        item.Text = myTable.Rows[j]["ModuleName"].ToString();
                        if (Convert.ToInt32(myTable.Rows[j]["ViewType"].ToString()) == 2)
                        {
                            item.Text = item.Text + "[必选]";
                        }
                        this.pklMyTable.ItemsRight.Add(item);
                    }
                }
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                try
                {
                    string str6 = string.Empty;
                    foreach (ListItem item2 in this.pklMyTable.ItemsLeft)
                    {
                        if (str6 == string.Empty)
                        {
                            str6 = item2.Value;
                        }
                        else
                        {
                            str6 = str6 + "," + item2.Value;
                        }
                    }
                    string str7 = " UPDATE [dbo].[Users] SET ";
                    if (this.ViewState["Pos"].ToString() == "left")
                    {
                        str7 = str7 + "[MytableLeft] = @Config";
                    }
                    else
                    {
                        str7 = str7 + "[MytableRight] = @Config";
                    }
                    SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@Config", SqlDbType.NVarChar, 200) };
                    commandParameters[0].Value = str6;
                    SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, str7, commandParameters);
                    this.litMessage.Text = base.ShowPrompt("桌面定义已保存", "mytable_config.aspx?pos=" + this.ViewState["Pos"].ToString());
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                    this.litMessage.Text = base.ShowPrompt("桌面定义保存失败", "mytable_config.aspx?pos=left");
                }
            }
        } 

    }
}
